<template>
  <a-modal v-model="visible" :mask-closable="false" width="40%" title="修改" :dialog-style="{ top: '20px' }" :footer="null">
    <div v-if="visible" v-permission="['system:tenant:modify']" v-loading="loading">
      <a-form-model ref="form" v-loading="loading" :label-col="{span: 4}" :wrapper-col="{span: 16}" :model="formData" :rules="rules">
        <a-form-model-item label="名称" prop="name">
          <a-input v-model="formData.name" allow-clear />
        </a-form-model-item>
        <a-form-model-item label="Jdbc Url" prop="jdbcUrl">
          <a-space v-if="!modifyJdbcUrl">
            <span>{{ oriFormData.jdbcUrl }}</span><a @click="modifyJdbcUrl = true">点此修改</a>
          </a-space>
          <a-input v-else v-model="formData.jdbcUrl" allow-clear />
        </a-form-model-item>
        <a-form-model-item label="Jdbc用户名" prop="jdbcUsername">
          <a-space v-if="!modifyJdbcUsername">
            <span>{{ oriFormData.jdbcUsername }}</span><a @click="modifyJdbcUsername = true">点此修改</a>
          </a-space>
          <a-input v-else v-model="formData.jdbcUsername" allow-clear />
        </a-form-model-item>
        <a-form-model-item label="Jdbc密码" prop="jdbcPassword">
          <a-space v-if="!modifyJdbcPassword">
            <span>{{ oriFormData.jdbcPassword }}</span><a @click="modifyJdbcPassword = true">点此修改</a>
          </a-space>
          <a-input v-else v-model="formData.jdbcPassword" allow-clear />
        </a-form-model-item>
        <a-form-model-item label="状态" prop="available">
          <a-select v-model="formData.available" allow-clear>
            <a-select-option v-for="item in $enums.AVAILABLE.values()" :key="item.code" :value="item.code">{{ item.desc }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <div class="form-modal-footer">
          <a-space>
            <a-button type="primary" :loading="loading" html-type="submit" @click="submit">保存</a-button>
            <a-button :loading="loading" @click="closeDialog">取消</a-button>
          </a-space>
        </div>
      </a-form-model>
    </div>
  </a-modal>
</template>
<script>

export default {
  // 使用组件
  components: {
  },

  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 是否可见
      visible: false,
      // 是否显示加载框
      loading: false,
      // 表单数据
      formData: {},
      oriFormData: {},
      modifyJdbcUrl: false,
      modifyJdbcUsername: false,
      modifyJdbcPassword: false,
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: '请输入名称' }
        ],
        available: [
          { required: true, message: '请选择状态' }
        ]
      }
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 打开对话框 由父页面触发
    openDialog() {
      this.visible = true

      this.$nextTick(() => this.open())
    },
    // 关闭对话框
    closeDialog() {
      this.visible = false
      this.$emit('close')
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {}
      this.oriFormData = {
        jdbcUrl: '',
        jdbcUsername: '',
        jdbcPassword: ''
      }
      this.modifyJdbcUrl = false
      this.modifyJdbcUsername = false
      this.modifyJdbcPassword = false
    },
    // 提交表单事件
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$api.system.tenant.modify(this.formData).then(() => {
            this.$msg.success('修改成功！')
            this.$emit('confirm')
            this.visible = false
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    // 页面显示时触发
    open() {
      // 初始化数据
      this.initFormData()

      // 查询数据
      this.loadFormData()
    },
    // 查询数据
    async loadFormData() {
      this.loading = true
      await this.$api.system.tenant.get(this.id).then(data => {
        this.formData = Object.assign({}, data)
        this.oriFormData = Object.assign({}, data)
        this.formData.jdbcUrl = ''
        this.formData.jdbcUsername = ''
        this.formData.jdbcPassword = ''
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
